import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

import javax.swing.*;
/*
 * Created by JFormDesigner on Sat Apr 23 11:47:47 CDT 2011
 */



/**
 * @author Becca Emrick
 */
public class OpenCoasterWindow extends JFrame {
//	private JList coasterList;
//	private JButton openButton;
//	private JButton cancelButton;
	
	public OpenCoasterWindow() {
		initComponents();
		
	}

	private void openCoaster(ActionEvent e) {
		String coasterName = (String) coasterList.getSelectedValue();
		//TODO add me in when database and glWindow are working
//		Rollercoaster coaster = GameManager.GM.database.getRollercoaster(coasterName);
//		GameManager.GM.glWindow.renderCoaster(coaster);
		this.setVisible(false);
		GameManager.GM.editor.setVisible(true);
	}

	private void cancelOpen(ActionEvent e) {
		this.setVisible(false);
		GameManager.GM.editor.setVisible(true);
	}
	
	public void addCoasterList(ArrayList<String> coasters) {
		DefaultListModel model = new DefaultListModel();
		coasterList.setModel(model);
		for(String coaster : coasters) {
			model.addElement(coaster);
		}
	}

	private void openWindowLostFocus(WindowEvent e) {
		// TODO add your code here
	}
	
	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		// Generated using JFormDesigner Evaluation license - Becca Emrick
		label1 = new JLabel();
		scrollPane1 = new JScrollPane();
		coasterList = new JList();
		openButton = new JButton();
		cancelButton = new JButton();

		//======== this ========
		addWindowFocusListener(new WindowAdapter() {
			@Override
			public void windowLostFocus(WindowEvent e) {
				openWindowLostFocus(e);
			}
		});
		Container contentPane = getContentPane();
		contentPane.setLayout(null);

		//---- label1 ----
		label1.setText("Choose a Coaster: ");
		contentPane.add(label1);
		label1.setBounds(new Rectangle(new Point(15, 10), label1.getPreferredSize()));

		//======== scrollPane1 ========
		{
			scrollPane1.setViewportView(coasterList);
		}
		contentPane.add(scrollPane1);
		scrollPane1.setBounds(15, 40, 255, 115);

		//---- openButton ----
		openButton.setText("Open");
		openButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				openCoaster(e);
			}
		});
		contentPane.add(openButton);
		openButton.setBounds(new Rectangle(new Point(215, 170), openButton.getPreferredSize()));

		//---- cancelButton ----
		cancelButton.setText("Cancel");
		cancelButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				cancelOpen(e);
			}
		});
		contentPane.add(cancelButton);
		cancelButton.setBounds(new Rectangle(new Point(140, 170), cancelButton.getPreferredSize()));

		{ // compute preferred size
			Dimension preferredSize = new Dimension();
			for(int i = 0; i < contentPane.getComponentCount(); i++) {
				Rectangle bounds = contentPane.getComponent(i).getBounds();
				preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
				preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
			}
			Insets insets = contentPane.getInsets();
			preferredSize.width += insets.right;
			preferredSize.height += insets.bottom;
			contentPane.setMinimumSize(preferredSize);
			contentPane.setPreferredSize(preferredSize);
		}
		pack();
		setLocationRelativeTo(getOwner());
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	// Generated using JFormDesigner Evaluation license - Becca Emrick
	private JLabel label1;
	private JScrollPane scrollPane1;
	private JList coasterList;
	private JButton openButton;
	private JButton cancelButton;
	// JFormDesigner - End of variables declaration  //GEN-END:variables
	
}
